package com.zhuojy.equity.mall.admin.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuojy.equity.mall.admin.common.bean.LogBean;
import com.zhuojy.equity.mall.admin.common.pojo.ResultEntity;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author xrz
 * @version 1.0
 * @description:TODO
 * @date 2024/3/26 19:29
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class LogService {

    @Autowired
    private MongoTemplate mongoTemplate;

    public ResultEntity findall(int pageSize, int pageNum) {
        // 分页参数
        Pageable pageable = PageRequest.of(pageNum, pageSize);

        // 构造查询条件
        Query query = new Query();

        // 查询总数
        long totalCount = mongoTemplate.count(query, LogBean.class);
        // 增加分页条件
        query.with(pageable);
        // 按照时间排序倒排
        query.with(Sort.by(Sort.Direction.DESC, "createDate"));
        List<LogBean> dtos = mongoTemplate.find(query, LogBean.class);
        // 构造分页返回
        Page<LogBean> page = new Page<>(pageNum, pageSize);
        page.setTotal(totalCount).setRecords(dtos);
        return ResultEntity.OK(page);
    }
}
